<template>
    <el-form :model="gouwuche" label-position="right" label-width="100">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="用户id" prop="yhid">
                    <el-input v-model="gouwuche.yhid" placeholder="请输入用户id" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="菜品id" prop="cpid">
                    <el-input v-model="gouwuche.cpid" placeholder="请输入菜品id" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="菜品数量" prop="cpsl">
                    <el-input v-model="gouwuche.cpsl" placeholder="请输入数量" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="菜品单价" prop="cpdj">
                    <el-input v-model="gouwuche.cpdj" placeholder="请输入单价" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="菜品总价" prop="cpzj">
                    <el-input v-model="gouwuche.cpzj" placeholder="请输入总价" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

    </el-form>
    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const gouwuche = ref({
    gwcid: '',
    yhid: '',
    cpid: '',
    cpsl: '',
    cpdj: '',
    cpzj: ''
})

// 取消
const doCancel = () => {
    // 触发事件：关闭 dialog
    emits("closeDialog");
}

// 新增
const doSave = async () => {
    const {data} = await axios.post('/api/gouwuche', gouwuche.value);
    console.log(data)
    if (data.code === 200) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>

<style scoped>

</style>